const aedes = require('aedes')()
const aedesServer = require('net').createServer(aedes.handle);
const  {log,logI}  = require('../log.js')
const port = 1883;
aedesServer.listen(port, function () {
    log.info('服务启动并开始监听端口：', port)
})

aedes.on("client", (client) => {
    log.info("客户端连接成功:", client.id);
})

//监听客户端发布的主题
aedes.on("publish", function (packet, client) {
    //针对不同的客户端写不同的逻辑
    //if (client) {
        //根据主题不同写不同的逻辑，建议采用JSON格式传输
        //logI("C-S", packet.topic, "MQTT", `${packet.payload.toString()}`, "null")
        let topic = packet.topic;
        switch (topic) {
            case 'RTMP_START':
                logI("C-S", packet.topic, "MQTT", `${packet.payload.toString()}`, "null")
                break;
            case 'RTMP_STOP':
                logI("C-S", packet.topic, "MQTT", `${packet.payload.toString()}`, "null")
                break;
        }
    //}
})